<template>
  <div>
    <button @click="connectWebsocketc">点击发起websocket请求</button>
    <button @click="closeWebsocketc">手动关闭websocket请求</button>
    <p v-if="show">{{ result }}</p>
  </div>
</template>
<script>
import { connectWebsocket,closeWebsocket } from '@/utils/websocket.js'
export default {
  data(){
    return {
      result:null,
      show:true
    }
  },
  methods:{
    connectWebsocketc(){
      let url = 'ws://localhost:8281'
      let agentData = { 'stocks': [ 'AAPL', 'MSFT', 'AMZN', 'GOOG', 'YHOO' ] }
      connectWebsocket(
        url,
        // 发送
        JSON.stringify(agentData),
        // 成功拿到后台返回的数据的回调函数
        data => {
          console.log('成功的回调函数, 接收到的data数据： ', JSON.parse(data))
          this.result = JSON.parse(data)
        },
        // websocket连接失败的回调函数
        () => {
          console.log('失败的回调函数')
        }
      )
    },
    closeWebsocketc(){
      this.show = false
      closeWebsocket()
    }
  }
}
</script>
<style lang="scss" scoped>

</style>
